package com.open.example.redis;

import com.open.redis.config.util.RedisBloom;
import com.open.redis.config.util.RedisLock;
import com.open.redis.config.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.UUID;


/**
 * @author: zm
 * @Date: 2025/4/5 16:30
 * @Version: V1.0
 * @description: |
 */

@Slf4j
@Component
public class RedisTestComponent implements CommandLineRunner {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedisBloom redisBloom;

    @Autowired
    RedisLock redisLock;

    public void redisUtil() {
        redisUtil.set("test", "test");
        log.info("" + redisUtil.get("test"));
    }

    /**
     * 需要redis安装BLOOM插件
     */
    public void redisBloom() {
        redisBloom.bfreserve("test", 0.01, 1000);
        String value1 = "value1";
        redisBloom.bfadd("test", value1);
        log.info("" + redisBloom.bfexists("test", value1));
        log.info("" + redisBloom.bfexists("test", "value2"));
        redisBloom.bfmadd("test", "value2", "value3");
        log.info("" + redisBloom.bfmexists("test", "value2", "value3", "value4"));
    }

    public void redisLock() {
        String key = "lock:key:" + UUID.randomUUID().toString();
        log.info("" + redisLock.lock(key, key, 1000L));
        log.info("" + redisLock.unLock(key, key));
    }

    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    public void run(final String... args) throws Exception {
        redisUtil();
        log.info("===============================");
        // redisBloom();
        log.info("===============================");
        redisLock();
    }
}
